<?php
/**
 * panx后台 管理产品标签和肤质检测项目的关系
 *
 * Author: lyun
 * Date: 2018/7/25
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Panx\Product;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\SkinDetectionItemUpdateRequest;
use App\Models\Admin\PanxAdmin;
use App\Models\Panx\ProductTag;
use App\Models\Panx\Tags\SkinDetectionItem;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class SkinDetectionItemController extends ApiController
{
    const ROUTES = [
        'index'  => 'api.admin.panx.skin.detection.item.index',
        'update' => 'api.admin.panx.skin.detection.item.update',
    ];

    /**
     * 肤质检测项目和产品标签的列表
     * @test
     *
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index()
    {
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        $items_group_by_detector_id = (new SkinDetectionItem)
            ->with('productTags', 'skinDetector')
            ->get()
            ->groupBy('skin_detector_id');

        $skin_detection_items = $this->transformSkinDetectionItem($items_group_by_detector_id);

        return $this->response(compact('skin_detection_items'));
    }

    /**
     * 返回所有的商品标签列表
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $product_tags = ProductTag::all();

        return $this->response(compact('product_tags'));
    }

    /**
     * 更新 产品标签和肤质检测项的关系
     * @test
     *
     * @param  SkinDetectionItemUpdateRequest  $request
     * @param  int  $skin_detection_item_id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(SkinDetectionItemUpdateRequest $request, $skin_detection_item_id)
    {
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var SkinDetectionItem $skin_detection_item */
        $skin_detection_item = SkinDetectionItem::find($skin_detection_item_id);
            $this->checkItemExist($skin_detection_item, trans('admin.skin_detection_item'));

        $product_tag_ids = $this->assembleProductTagIds($request->product_tag_ids);

        $updated = $skin_detection_item->productTags()->sync($product_tag_ids);

        return $this->response(compact('updated'));
    }

    /**
     * @param Collection $skin_detection_items
     * @return mixed
     */
    private function transformSkinDetectionItem(Collection $skin_detection_items)
    {
        return $skin_detection_items->transform(function (Collection $items, $index) {
            $data = [
                'skin_detector_id'     => $index,
                'skin_detector_type'   => $items[0]->skinDetector->type,
                'skin_detector_remark' => $items[0]->skinDetector->remark,
                'skin_detector_items'  => $items,
            ];
            $items->makeHidden('skinDetector');

            return $data;
        })
            ->values();
    }

    /**
     * @param $product_tag_ids
     * @return array
     */
    private function assembleProductTagIds($product_tag_ids)
    {
        if (!is_array($product_tag_ids)) return [];

        return array_values($product_tag_ids);
    }
}
